package cli.command;

import cli.state.CurrentPath;

import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;

public class ChangeDirectory implements Command {
    private PrintWriter out;
    private CurrentPath currentPath;

    public ChangeDirectory(PrintWriter out, CurrentPath currentPath) {
        this.out = out;
        this.currentPath = currentPath;
    }

    @Override
    public void handle(String[] args) {
        if (args.length == 0) {
            out.println("请输入路径！");
            return;
        }
        Path path = currentPath.get().resolve(args[0]);
        if (!Files.isDirectory(path)) {
            out.println("文件夹不存在！");
            return;
        }
        if (!currentPath.set(path)) {
            out.println("切换失败！");
        }
    }
}
